package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class HznewsNewsPaperAnalyse implements AnalyseNews {

	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://e.hznews.com/paper/hzrb/20121204/A1/1/
		String[] regex = {
				"http://e.hznews.com/paper/djsb/[0-9]{8}/[A-Z0-9]*/[0-9]*[/]{0,1}",
				"http://e.hznews.com/paper/hzrb/[0-9]{8}/[A-Z0-9]*/[0-9]*/"
				
		};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		
		newspaper.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
		
		title =doc.select("html body div.alyout div.main div.left div.border div.round div.content h2").text();
		
		date=DateUtils.matchDate(doc.select("html body div.alyout div.main div.left div.border div.round div.content div.msg div.info").text());
		content = HtmlCleaner.getContentHtml(url,doc.select("html body div.alyout div.main div.left div.border div.round div.nr div.cnt-main"));
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);
		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		//http://e.hznews.com/paper/hzrb/20121204/A5/
//		/http://e.hznews.com/paper/djsb/20130704/A04/
		String[] regex = {
				"http://e.hznews.com/paper/djsb/[0-9]{8}/[A-Z0-9]*[/]{0,1}",
				"http://e.hznews.com/paper/hzrb/[0-9]{8}/[A-Z0-9]*/"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	

}
